﻿using KingerRobot.MexicoSailunMES.Spi.DTO;
using System;
using System.Text.RegularExpressions;

namespace KingerRobot.MexicoSailunWMS.SpiService;

internal class BMesProduct : IMesProduct
{
    public TireInfo GetInfoByBarcode(string productCode)
    {
        var db = Db.Default;
        if (productCode is null)
        {
            return null;
        }
        var product = db.MesProducts.Where(t => t.BcdNo == productCode)
            .OrderByDescending(t => t.CreateTime)
            .Select(t => new TireInfo
            {
                TireCode = t.BcdNo,
                MaterialCode = t.MaterialCode,
                MaterialId = t.MaterialId ?? 0,
                //Height = Convert.ToInt32(t.HEIGHT ?? 0),
                Size = GetSize(t.TbmSpecDesc),
                ProdDt = t.ProdDt.Value,
                TbmNo = t.TbmNo,
                TbmPs = t.TbmPs,
                InPermit = t.InPermit,
                TbmExpirationTime = t.TbmExpirationTime
            }).FirstOrDefault();
        return product;
    }
    /// <summary>
    /// 从规格描述中获取字母R后面的数字，即为尺寸
    /// </summary>
    /// <returns></returns>
    public static double GetSize(string materialDesc)
    {
        Match match = Regex.Match(materialDesc, @"R(\d+(?:\.\d+)?)");
        if (match.Success)
        {
            string text = match.Groups[1].Value;
            if (double.TryParse(text, out double size))
            {
                return size;
            }
        }

        return 0;
    }


}

